package com.gimi.cloud.bbp.task.service.impl;

import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.log.LogScheduleJobDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.log.LogScheduleJobDTO;
import com.gimi.cloud.bbp.entity.log.LogScheduleJobEntity;
import com.gimi.cloud.bbp.task.service.ScheduleJobLogService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务日志相关接口实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-08
 **/
@Service
public class ScheduleJobLogServiceImpl implements ScheduleJobLogService{
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobLogServiceImpl.class);
    private LogScheduleJobDao logScheduleJobDao;

    @Autowired
    public ScheduleJobLogServiceImpl(LogScheduleJobDao logScheduleJobDao) {
        this.logScheduleJobDao = logScheduleJobDao;
    }

    @Override
    public List<LogScheduleJobDTO> queryList(LogScheduleJobDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT log.*,job.job_key,job.job_title" +
                " FROM log_schedule_job log" +
                " LEFT JOIN schedule_job job ON job.is_delete = 0 AND job.job_id = log.job_id" +
                " WHERE 1 = 1 AND job.job_key LIKE '%'||:jobKey||'%' AND job.job_title LIKE '%'||:jobTitle||'%' " +
                " ORDER BY log.lsg_start_time DESC";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(dto);
        return logScheduleJobDao.selectListDTOBySql(queryParam);

    }

    @Override
    public void save(LogScheduleJobDTO dto) throws Exception {
        LogScheduleJobEntity entity = new LogScheduleJobEntity();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,entity);
        logScheduleJobDao.baseInsert(entity);
    }
}
